﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunSpace.Application.OrderFlowManage;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.OrderFlowManage;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.ThirdService;

namespace YunSpace.Application.Email
{
    public class SendMessage
    {
        /// <summary>
        /// 发送申请通知
        /// </summary>
        /// <param name="ActivityTheme">场地名称</param>
        /// <param name="WorkFlowName">流程名称</param>
        /// <param name="Email">审核人邮箱</param>
        /// <param name="Phone">审核人手机</param>
        /// <returns></returns>
        public static bool SendMessageToApply(string ActivityTheme,string WorkFlowName,List<string> Email,List<string> CC,List<string> Phone)
        {
            try
            {
                var currentUser = OperatorProvider.Provider.GetCurrent();
                Email = Email.Distinct().ToList();
                CC = CC.Distinct().ToList();
                Phone = Phone.Distinct().ToList();
                //发送邮件                
                MailHelper mail = new MailHelper();
                mail.MailServer = Configs.GetValue("MailHost");
                mail.MailUserName = currentUser.Email;
                mail.MailPassword = currentUser.EmailPassword;
                mail.MailName = "申请通知";
                var name = currentUser.UserCode + "(" + currentUser.UserName + ")";
                var EmailBody = String.Format("【YUNSPACE】申请通知，{0}申请的{1}，已经到了【{2}】阶段，需要您的审核.YUNSPACE您身边的场地专家！",name, ActivityTheme, WorkFlowName);
                foreach (var item in Email)
                {
                    var EmailBool = mail.SendToAttachment(item, "申请审核通知", EmailBody, "", CC);
                }


                ////发送短信
                RongLianSmsManger sms = new RongLianSmsManger();

                var smsData = new String[] { name, ActivityTheme,WorkFlowName };
                sms.SendApplyInfo(String.Join(",",Phone), smsData);              
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 发送审核通知
        /// </summary>
        /// <returns></returns>
        public static bool SendMessageToAuditing(string ActivityTheme, string WorkFlowName,string Result, List<string> Email, List<string> CC, List<string> Phone)
        {
            //发送邮件                
            var currentUser = OperatorProvider.Provider.GetCurrent();
            var AuditingName = currentUser.UserCode + "(" + currentUser.UserName + ")";
            Email = Email.Distinct().ToList();
            CC = CC.Distinct().ToList();
            Phone = Phone.Distinct().ToList();
            //发送邮件                
            MailHelper mail = new MailHelper();
            mail.MailServer = Configs.GetValue("MailHost");
            mail.MailUserName = currentUser.Email;
            mail.MailPassword = currentUser.EmailPassword;
            mail.MailName = "审核结果通知";
            var EmailBody = String.Format("【YUNSPACE】审核通知，您申请的{0},在【{1}】阶段已经被{2}审核，审核结果为:{3},请及时处理下一步操作。YUNSPACE您身边的场地专家！", ActivityTheme, WorkFlowName, AuditingName,Result);
            foreach (var item in Email)
            {
                var EmailBool = mail.SendToAttachment(item, "审核结果通知", EmailBody, "",CC);
            }
           

            //发送短信
            RongLianSmsManger sms = new RongLianSmsManger();
            var smsData = new String[] { ActivityTheme, WorkFlowName, AuditingName,Result };
            sms.SendAuditingInfo(String.Join(",", Phone), smsData);
            return true;
        }



        public static void Send(string F_FlowId, int ApplyType, string OrderId, string F_CheckId, string F_No, int ck_F_OrderState, string SiteIds,int State)
        {
            OrderCheckApp app = new OrderCheckApp();
            List<CheckRecordEntity> checkList =app.GetCheckRecordEntityList(OrderId, ApplyType, F_No);
            SysWorkFlowDetailEntity entity =app.GetSysWorkFlowDetailEntity(F_FlowId, ApplyType);
            var checkEntity = checkList.Find(t => t.F_Id == F_CheckId);
            WorkFlow.FlowManageApp flow = new WorkFlow.FlowManageApp();
            using (var db = new RepositoryBase())
            {
                var UserId = checkEntity.F_ApplyUserId;
                var Send = db.FindEntity<UserEntity>(t => t.F_Id == UserId && t.F_EnabledMark == true);
                var CCList = flow.GetUserId(entity.F_WorkFlowNo ?? 0, entity.F_CcOperationPType, db, entity.F_CcUserId, OrderId, F_No, SiteIds);
                var ActivityTheme = new List<string>();
                var SiteIdArr = SiteIds.Split(',');
                foreach (var it in SiteIdArr)
                {
                    var Site = db.FindEntity<Domain.Entity.SiteLibraryManage.SiteEntity>(t => t.F_Id == it);
                    ActivityTheme.Add(Site.title);
                }
                var Phone = new List<String>();
                Phone.Add(Send.F_MobilePhone);
                Phone.AddRange(CCList.Select(t => t.Item4));
                var WorkFlowName = checkEntity.F_CheckContent;
                var ShangHu = Configs.GetValue("MailMerchants");
                var YinXiao = Configs.GetValue("MailSales");
                var FaZhan = Configs.GetValue("MailSms");
                var CCEmail = CCList.Select(t => t.Item3).ToList();
                CCEmail.Add(ShangHu);
                CCEmail.Add(YinXiao);
                CCEmail.Add(FaZhan);
                var Result = State == 1 ? "通过" : "驳回";
                SendMessageToAuditing(string.Join(",", ActivityTheme), WorkFlowName, Result, new List<string>() { Send.F_Email }, CCEmail, Phone);
            }
        }
    }
}
